//
//  FilesEnum.swift
//  ImageDecode
//
//  Created by delouch on 2024/9/24.
//

import Foundation
import AVFoundation

struct FoldInfo {
    let path : String
    
    var fileList : [FileInfo]
    
}

struct FileInfo {
    let size : Int
    
    let name : String
    
    let fileType : String
    
    let path : String
    
    let duration : Double
    
    let fold : String
    
    let step : Int
    
    init(size: Int, name: String, fileType: String, path: String, duration : Double = 0, fold : String = "", step : Int = 0) {
        self.size = size
        self.name = name
        self.fileType = fileType
        self.path = path
        let videoAsset : AVURLAsset = AVURLAsset(url: URL.init(filePath: path))
        self.duration = Double(videoAsset.duration.value) / Double(videoAsset.duration.timescale)
        self.fold = path.replacingOccurrences(of: name + "." + fileType, with: "")
        self.step = Int(ceil(self.duration / 600.0))
    }
    
}

class FilesEnum: NSObject {
    
    static func enumerateFiles(at path: String, list : inout [FileInfo]) {
        let fileManager = FileManager.default
        do {
            let files = try fileManager.contentsOfDirectory(atPath: path)
            for file in files {
                let filePath = (path as NSString).appendingPathComponent(file)
                let attributes = try fileManager.attributesOfItem(atPath: filePath)
                if let isDir = attributes[FileAttributeKey.type] as? FileAttributeType, isDir == .typeDirectory {
                    enumerateFiles(at: filePath, list: &list)
                }else {
                    if let fileSize = attributes[FileAttributeKey.size] as? Int, !file.contains("DS_Store") {
                        if let fileType = file.components(separatedBy: ".").last {
                            list.append(FileInfo(size: fileSize, name: file.replacingOccurrences(of: ".\(fileType)", with: ""), fileType: fileType, path: filePath))
                        }
                    }
                }
            }
        } catch {
            print("Error: \(error)")
        }
    }
    
    static func enumerateFolds(at path: String, list : inout [FoldInfo]) {
        let fileManager = FileManager.default
        do {
            let files = try fileManager.contentsOfDirectory(atPath: path)
            for file in files {
                let filePath = (path as NSString).appendingPathComponent(file)
                let attributes = try fileManager.attributesOfItem(atPath: filePath)
                if let isDir = attributes[FileAttributeKey.type] as? FileAttributeType, isDir == .typeDirectory {
                    if !file.hasSuffix(".appiconset") {
                        enumerateFolds(at: filePath, list: &list)
                    }
                }else {
                    if let fileSize = attributes[FileAttributeKey.size] as? Int {
//                        list.append(FileInfo(size: fileSize / 1024, name: file, path: filePath))
                    }
                }
            }
        } catch {
            print("Error: \(error)")
        }
    }
    
}
